package com.hiyee.platform.service.impl;

import com.hiyee.platform.mapper.BaseCityCodeMapper;
import com.hiyee.platform.pojo.BaseCityCode;
import com.hiyee.platform.service.FileUploadService;
import com.hiyee.platform.util.ExcelUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    BaseCityCodeMapper baseCityCodeMapper;

    @Override
    public void handle(String action, String filePath) {

        // 读取 excel 文档数据
        List<Map<Integer, String>> excelData = null;
        try {
             excelData = ExcelUtil.read(filePath);
        }catch (InvalidFormatException | IOException e) {
            e.printStackTrace();
        }
        if(excelData != null) {
            switch (action) {
                case "CITY_CODE":
                    for (Map<Integer, String>city : excelData) {
                        BaseCityCode baseCityCode = new BaseCityCode();
                        baseCityCode.setCityId(0);
                        baseCityCode.setCitycode(city.get(2));
                        baseCityCode.setAdcode(Integer.parseInt(city.get(1)));
                        baseCityCode.setName(city.get(0));
                        baseCityCode.setCreatedAt(new Date());
                        baseCityCodeMapper.insert(baseCityCode);
                    }
                    break;
            }
        } else {
            System.out.println("[Excel文件导入] " + filePath + "文件读取数据为空！");
        }
    }
}
